﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using IOEntity;

namespace IODataAccess
{
    public class clsDATypeDetail
    {
        clsDAConnection con = new clsDAConnection();
        public clsTypeDetail GetById(int id)
        {
            try
            {
                clsTypeDetail obj = new clsTypeDetail();
                string str = "select * from tblTypeDetail where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@id", id);
                con.openCon();
                SqlDataReader dr = com.ExecuteReader();
                if (dr.Read())
                {
                    obj.Id = id;
                    if (dr["tieude"] != DBNull.Value)
                    {
                        obj.TieuDe = dr["tieude"].ToString();
                    }
                    if (dr["stt"] != DBNull.Value)
                    {
                        obj.Stt = Convert.ToInt32(dr["stt"].ToString());
                    }
                }
                return obj;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public List<clsTypeDetail> GetAll()
        {
            try
            {
                List<clsTypeDetail> list = new List<clsTypeDetail>();
                string str = "select * from tblTypeDetail";
                SqlCommand com = new SqlCommand(str, con.getCon());
                con.openCon();
                SqlDataReader dr = com.ExecuteReader();
                while (dr.Read())
                {
                    clsTypeDetail obj = new clsTypeDetail();
                    if (dr["tieude"] != DBNull.Value)
                    {
                        obj.TieuDe = dr["tieude"].ToString();
                    }
                    if (dr["stt"] != DBNull.Value)
                    {
                        obj.Stt = Convert.ToInt32(dr["stt"].ToString());
                    }
                    if (dr["id"] != DBNull.Value)
                    {
                        obj.Id = Convert.ToInt32(dr["id"].ToString());
                    }
                    list.Add(obj);
                }
                return list;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public int Insert(clsTypeDetail obj)
        {
            try
            {
                string str = "insert into tblTypeDetail(tieude,stt) values(@tieude,@stt)";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@tieude", obj.TieuDe);
                com.Parameters.AddWithValue("@stt", obj.Stt);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
                return clsDAStaticMethod.getIDIdentity("tblTypeDetail");
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public void Update(clsTypeDetail obj)
        {
            try
            {
                string str = "Update tblTypeDetail set tieude=@tieude,stt=@stt where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@tieude", obj.TieuDe);
                com.Parameters.AddWithValue("@stt", obj.Stt);
                com.Parameters.AddWithValue("@id", obj.Id);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public void Delete(int id)
        {
            try
            {
                string str = "delete from tbltypedetail where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@id", id);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
    }
}
